/* eslint-disable prettier/prettier */
import { messages, isEmptyObj } from "share/common";
import React, { Component } from "react";
import { Checkbox, Form, Button, Input, Spin } from "antd";
import CheckboxRadio from "../components/checkbox-radio";
import HotelServer from "../hotel-server";
import HotelConfigUtil from "../hotel-config-util";
import { message } from "antd/lib/index";
import ReasonSetting from "../../reasons-config/reasons-setting";



const FormItem = Form.Item;
const BoxGroup = Checkbox.Group;
const TextArea = Input.TextArea;

@Form.create()
class SetTravelStandard extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      controlData: {}, // 管控设置数据
      aboutAgreement: false, // 协议价酒店
      aboutApproval: false, // 审批单管控
      hotelTravelApprlval: {}, // 审批单管控
      travelControl: false, // 差标管控
      travelControlData: {}, // 差标管控设置数据
    };
  }

  componentWillMount() {
    this.initData();
  }

  initData = () => {
    HotelServer(this)
      .getHotelControlData({ companyOid: this.props.companyOID })
      .then((res) => {
        const { body } = res.data;
        this.setState(
          {
            controlData: body,
            travelControl: body.enabledTravelStandardsControl,
            travelControlData: body.travelStandardsControl,
            aboutAgreement: body.controlAgreement,
            aboutApproval: body.enabledTravelApprovalControl,
            hotelTravelApprlval: body.travelApprovalControl,
          },
          this.props.form.resetFields()
        );
      });
  };

  // 保存配置
  onSaveControlData = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const { controlData } = this.state;
        const params = HotelConfigUtil.formatControlData(controlData, values);
        params.companyOid = this.props.companyOID;
        params.reasonForNotAgreement = null;
        params.reasonForBeyondStandards = null;
        HotelServer(this)
          .saveHotelControlData(params)
          .then((res) => {
            if (res.data.success) {
              this.initData();
              message.success(messages("supplier-1.key355") /* 保存成功*/);
            } else {
              message.error(res.data.message);
            }
          });
      }
    });
  };

  /**
   * 协议价启用开关
   * @param values
   */
  relationReasonForNotAgreement = (values) => {
    this.setState({ aboutAgreement: values.controlAgreement });
  };

  /**
   * 审批单管控启用开关
   * @param values
   */
  onApprovalChange = (values) => {
    this.setState({ aboutApproval: values.target.checked });
  };

  /**
   * 是否启用差标管控
   * @param value
   */
  onTravelControlChange = (value) => {
    this.setState({ travelControl: value.target.checked });
  };

  // 审批单选人员组自定义检验
  approvalUserGroupChecked = (rule, value, callback) => {
    if (value.enabledNoApproval && value.noApprovalUserGroupId_ === "userId" && !value.noApprovalUserGroupId) {
      callback(messages("supplier-1.key408") /* 人员组必填*/);
    } else if (value.noApprovalUserGroupId_ === "all" || value.noApprovalUserGroupId) {
      callback();
    }
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { controlData, aboutAgreement, aboutApproval, hotelTravelApprlval } = this.state;
    const { travelControl, travelControlData, loading } = this.state;
    return (
      <div className="airline-config-container">
        <Spin spinning={loading}>
          {!isEmptyObj(controlData) &&<form>
            <h2>{messages("supplier-1.key491") /* 房型屏蔽设置*/}</h2>
            <a className="a-line-bottom">{messages("supplier-1.key492") /* 员工查询时可屏蔽相应房型*/}</a>
            <FormItem>
              {getFieldDecorator("shieldRoomType", {
                initialValue: controlData.shieldRoomType === "1",
                valuePropName: "checked",
              })(<Checkbox>{messages("supplier-1.key493") /* 屏蔽行政房*/}</Checkbox>)}
            </FormItem>
            <br />
            <h2>{messages("supplier-1.key494") /* 房价屏蔽设置*/}</h2>
            <a className="a-line-bottom">{messages("supplier-1.key495") /* 启用后，可根据差标屏蔽相应酒店/房价*/}</a>
            <FormItem>
              {getFieldDecorator("shieldByTravelStandard", {
                initialValue: controlData.shieldByTravelStandard || false,
                valuePropName: "checked",
              })(<Checkbox>{messages("supplier-1.key35") /* 启用temp*/}</Checkbox>)}
            </FormItem>
            <br />
            <h2>{messages("supplier-1.key417") /*重复预订控制*/}</h2>
            <a className="a-line-bottom">{messages("supplier-1.key759") /*启用后同一入住人不可重复下单*/}</a>
            <FormItem>
              {getFieldDecorator("enabledCheckRepeatBooking", {
                initialValue: controlData.enabledCheckRepeatBooking || false,
                valuePropName: "checked",
              })(<Checkbox>{messages("supplier-1.key35") /* 启用temp*/}</Checkbox>)}
            </FormItem>
            <br />
            <h2>{messages("supplier-1.key496") /* 协议价预订控制*/}</h2>
            <a className="a-line-bottom">{messages("supplier-1.key497") /* 启用后，可管控员工预订协议酒店协议房型*/}</a>
            <FormItem>
              {getFieldDecorator("agreement", {
                initialValue: {
                  controlAgreement: controlData.controlAgreement || false,
                  controlAgreementWay: controlData.controlAgreementWay,
                },
              })(
                <CheckboxRadio
                  onChange={this.relationReasonForNotAgreement}
                  {...HotelConfigUtil.getAgreementForm(controlData)}
                />
              )}
            </FormItem>
            {aboutAgreement && (
              <FormItem>
                {getFieldDecorator("reasonForNotAgreementList", {
                  initialValue: controlData.reasonForNotAgreementList || [],
                })(<ReasonSetting
                  source="hotel"
                  companyOID = {this.props.companyOID}
                 />
                )}
              </FormItem>
            )}
            <br />
            <h2>{messages("supplier-1.key217") /* 审批单管控*/}</h2>
            <a>
              {
                messages(
                  "supplier-1.key428"
                ) /* 启用后，员工需提前申请，预订时需关联申请单。提前审批模式不支持该管控方式*/
              }
            </a>
            <a className="a-line-bottom">{messages("supplier-1.key501") /* 强管控：禁止提交；若管控：需提交理由*/}</a>
            <FormItem>
              {getFieldDecorator("enabledTravelApprovalControl", {
                initialValue: controlData.enabledTravelApprovalControl,
                valuePropName: "checked",
              })(<Checkbox onChange={this.onApprovalChange}>{messages("supplier-1.key35") /* 启用temp*/}</Checkbox>)}
            </FormItem>
            {aboutApproval && (
              <FormItem className="form-item-style" style={{ width: "90%" }}>
                {getFieldDecorator("approvalList", {
                  rules: [{ validator: this.approvalUserGroupChecked }],
                  initialValue: {
                    enabledNoApproval: hotelTravelApprlval.enabledNoApproval || false,
                    noApprovalUserGroupId_: hotelTravelApprlval.noApprovalUserGroupId != "all" ? "userId" : "all",
                    noApprovalUserGroupId:
                      hotelTravelApprlval.noApprovalUserGroupId != "all"
                        ? hotelTravelApprlval.noApprovalUserGroupId
                        : "",
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("list")} />)}
              </FormItem>
            )}
            {aboutApproval && (
              <FormItem>
                {getFieldDecorator("approvalDate", {
                  initialValue: {
                    controlDate: hotelTravelApprlval.controlDate || false,
                    controlDateWay: hotelTravelApprlval.controlDateWay,
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("date")} />)}
              </FormItem>
            )}
            {aboutApproval && (
              <FormItem>
                {getFieldDecorator("approvalNight", {
                  initialValue: {
                    controlRoomNight: hotelTravelApprlval.controlRoomNight || false,
                    controlRoomNightWay: hotelTravelApprlval.controlRoomNightWay,
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("night")} />)}
              </FormItem>
            )}
            {aboutApproval && (
              <FormItem>
                {getFieldDecorator("approvalSite", {
                  initialValue: {
                    controlSite: hotelTravelApprlval.controlSite || false,
                    controlSiteWay: hotelTravelApprlval.controlSiteWay,
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("site")} />)}
              </FormItem>
            )}
            {aboutApproval && (
              <FormItem>
                {getFieldDecorator("approvalAmount", {
                  initialValue: {
                    controlMaxPrice: hotelTravelApprlval.controlMaxPrice || false,
                    controlMaxPriceWay: hotelTravelApprlval.controlMaxPriceWay,
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("amount")} />)}
              </FormItem>
            )}
            {aboutApproval && (
              <FormItem>
                {getFieldDecorator("approvalPeople", {
                  initialValue: {
                    controlGuest: hotelTravelApprlval.controlGuest || false,
                    controlGuestWay: hotelTravelApprlval.controlGuestWay,
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("people")} />)}
              </FormItem>
            )}
            {aboutApproval && (
              <FormItem>
                {getFieldDecorator("enabledDynamicAmount", {
                  initialValue: hotelTravelApprlval.enabledDynamicAmount || false,
                  valuePropName: "checked",
                })(
                  <Checkbox>
                    {messages("supplier-1.key502") /* 金额动态管控（以实际入住城市动态计算可定金额）*/}
                  </Checkbox>
                )}
              </FormItem>
            )}
            <br />
            <h2>{messages("supplier-1.key345") /* 差标管控*/}</h2>
            <a className="a-line-bottom">
              {messages("supplier-1.key503") /* 差标可在【费用与报销管控-费控标准-差旅标准】进行设置*/}
            </a>
            <FormItem>
              {getFieldDecorator("enabledTravelStandardsControl", {
                initialValue: controlData.enabledTravelStandardsControl,
                valuePropName: "checked",
              })(
                <Checkbox onChange={this.onTravelControlChange}>{messages("supplier-1.key35") /* 启用temp*/}</Checkbox>
              )}
            </FormItem>
            {travelControl && (
              <FormItem>
                {getFieldDecorator("travelStar", {
                  initialValue: {
                    controlStar: travelControlData.controlStar || false,
                    controlStarWay: travelControlData.controlStarWay,
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("star")} />)}
              </FormItem>
            )}
            {travelControl && (
              <FormItem>
                {getFieldDecorator("travelPrice", {
                  initialValue: {
                    controlPrice: travelControlData.controlPrice || false,
                    controlPriceWay: travelControlData.controlPriceWay,
                  },
                })(<CheckboxRadio {...HotelConfigUtil.getApprovalForm("price")} />)}
              </FormItem>
            )}
            <br />
            <FormItem>
              {getFieldDecorator("reasonForBeyondStandardsList", {
                initialValue: controlData.reasonForBeyondStandardsList || []
              })(<ReasonSetting
                companyOID = {this.props.companyOID}
                source="hotel"
                subTitle={messages('supplier-1.key853')/*请选择超标理由*/}
                title = {messages("supplier-1.key438") /* 超标理由*/}
               />
              )}
            </FormItem>
            <br />
            <br />
            <br />
            <h2>{messages('supplier-1.key440')/*超标提示语*/}</h2>
            <a className="a-line-bottom">
              {messages("supplier-1.key439") /* 超标提示语：可在进行超标提示时，提示公司展示公司政策*/}
            </a>
            <FormItem style={{ marginTop: 18 }}>
              {getFieldDecorator("promptForBeyondStandards", {
                initialValue: controlData.promptForBeyondStandards || "",
              })(<TextArea placeholder={messages("supplier-1.key440") /* 超标提示语*/} />)}
            </FormItem>
          </form>}

          <br />
          <br />
          <br />
          <Button type="primary" onClick={this.onSaveControlData}>
            {messages("supplier-1.key356") /* 保存配置*/}
          </Button>
          <br />
          <br />
          <br />
        </Spin>
      </div>
    );
  }
}

export default SetTravelStandard;
